package Jets.ui.joystick
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.utils.getTimer;
	
	import Jets.display.JSprite;
	import Jets.events.JJoystickEvent;
	import Jets.events.JTickEvent;
	
	public class JJoystick extends JSprite
	{
		public static var Delay:int = 100;

		private var ms_backShape:Sprite;
		private var ms_cPoint:Sprite;
		private var ms_mouseStatus:int = 0;
		private var ms_centerX:Number = 0;
		private var ms_centerY:Number = 0;
		private var ms_saveTime:int = 0;
		private var ms_bSize:Number;
		private var ms_pSize:Number;
		private var ms_bColor:Number;
		private var ms_pColor:Number;
		private var ms_Distance:Number;

		/**
		 * 摇杆控制器界面 具有八方向，响应时间可以通过JJoystick.Delay来进行调整
		 * @param backSize 背景圆圈的大小
		 * @param ptSize 中间圆圈的大小
		 * @param bColor 背景的颜色值
		 * @param pColor 中间圆圈的颜色值
		 * @param distance 唯一误差最小值，这个值设置太大检测会不灵敏
		 * 
		 */
		public function JJoystick(backSize:Number=100,ptSize:Number=30,bColor:uint=0xa1a3a6,pColor:uint=0xed1941,distance:int = 5)
		{
			super();
			ms_bSize = backSize;
			ms_bColor = bColor;
			ms_pSize = ptSize;
			ms_pColor = pColor;
			ms_Distance = distance;
			
		}
		
		/**
		 * 设置背景的alpha值
		 */
		public function set backgroundAlpha(value:Number):void
		{
			ms_backShape.alpha = value;
		}
		
		public function set backFilter(arr:Array):void
		{
			ms_backShape.filters = arr;
		}
		
		public function set pointFilter(arr:Array):void
		{
			ms_cPoint.filters = arr;
		}
		
		/**
		 * 设置中间圆圈的alpha值
		 */
		public function set pointAlpha(value:Number):void
		{
			ms_cPoint.alpha = value;
		}
		
		protected function onMouseUpHandler(event:MouseEvent):void
		{
			if(ms_mouseStatus == 1)
			{
				ms_cPoint.stopDrag();
				ms_cPoint.x = ms_centerX;
				ms_cPoint.y = ms_centerY;
				ms_mouseStatus = 0;
			}
		}
		
		protected function onMouseOutHandler(event:MouseEvent):void
		{
			if(ms_mouseStatus == 1)
			{
				ms_cPoint.stopDrag();
				ms_cPoint.x = ms_centerX;
				ms_cPoint.y = ms_centerY;
				ms_mouseStatus = 0;
			}
		}
		
		protected function onMouseDownHandler(event:MouseEvent):void
		{
			if(ms_mouseStatus == 0 )
			{
				ms_cPoint.startDrag(true,getBounds(ms_backShape));
				ms_mouseStatus = 1;
			}
		}
		
		override protected function onAddStageEvent():void
		{
			ms_backShape = new Sprite();
			ms_cPoint = new Sprite();
			
			ms_backShape.cacheAsBitmap = true;
			ms_cPoint.cacheAsBitmap = true;
			ms_backShape.mouseEnabled = false;
			ms_cPoint.mouseEnabled = false;
			
			this.addChild(ms_backShape);
			this.addChild(ms_cPoint);
			
			ms_backShape.graphics.clear();
			ms_backShape.graphics.beginFill(ms_bColor,1);
			ms_backShape.graphics.drawCircle(ms_bSize,ms_bSize,ms_bSize);
			ms_backShape.graphics.endFill();
			
			ms_cPoint.graphics.clear();
			ms_cPoint.graphics.beginFill(ms_pColor,1);
			ms_cPoint.graphics.drawCircle(0,0,ms_pSize);
			ms_cPoint.graphics.endFill();
		
			ms_cPoint.x  = ms_backShape.x + ms_pSize + (ms_backShape.width - ms_cPoint.width)/2;
			ms_cPoint.y  = ms_backShape.y + ms_pSize +(ms_backShape.height - ms_cPoint.height)/2;
			
			ms_centerX = ms_backShape.width/2;
			ms_centerY = ms_backShape.height/2;
				
			//this.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
			//this.addEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler);
			//this.addEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
			this.addEventListener(TouchEvent.TOUCH_BEGIN,onTouchBeginHandler);
			this.addEventListener(TouchEvent.TOUCH_OUT,onTouchOutHandler);
			this.addEventListener(TouchEvent.TOUCH_END,onTouchEndHandler);
			
			this.TickEnable = true;
		}
		
		protected function onTouchEndHandler(event:TouchEvent):void
		{
			if(ms_mouseStatus == 1)
			{
				ms_cPoint.stopDrag();
				ms_cPoint.x = ms_centerX;
				ms_cPoint.y = ms_centerY;
				ms_mouseStatus = 0;
			}
			
		}
		
		protected function onTouchOutHandler(event:TouchEvent):void
		{
			if(ms_mouseStatus == 1)
			{
				ms_cPoint.stopDrag();
				ms_cPoint.x = ms_centerX;
				ms_cPoint.y = ms_centerY;
				ms_mouseStatus = 0;
			}
			
		}
		
		protected function onTouchBeginHandler(event:TouchEvent):void
		{
			if(ms_mouseStatus == 0 )
			{
				ms_cPoint.startDrag(true,getBounds(ms_backShape));
				ms_mouseStatus = 1;
			}
			
		}		
		
		override protected function onTickEvent(delay:Number):void
		{
			var t:Number = getTimer();
			if(ms_mouseStatus == 1 && (ms_saveTime==0 || t-ms_saveTime>= Delay))
			{
				ms_saveTime = t;
				var evt:JJoystickEvent = new JJoystickEvent(JJoystickEvent.ON_CONTROL_EVENT);
				var cx:Number = ms_cPoint.x;
				var cy:Number = ms_cPoint.y;
				//左边
				if(cx + ms_Distance < ms_centerX)
				{
					if(cy+ms_Distance < ms_centerY)
					{
						//左上
						evt.keyCode = JJoystickCode.UP_LEFT;
					}
					else if(cy-ms_Distance > ms_centerY)
					{
						//左下	
						evt.keyCode = JJoystickCode.DOWN_LEFT;
					}
					else
					{
						//左
						evt.keyCode = JJoystickCode.LEFT;
					}
				}
				else if(cx - ms_Distance > ms_centerX)
				{
					if(cy+ms_Distance < ms_centerY)
					{
						//右上
						evt.keyCode = JJoystickCode.UP_RIGHT;
					}
					else if(cy-ms_Distance > ms_centerY)
					{
						//右下
						evt.keyCode = JJoystickCode.DOWN_RIGHT;
					}
					else
					{
						//右
						evt.keyCode = JJoystickCode.RIGHT;
					}
				}
				else
				{
					if(cy+ms_Distance < ms_centerY)
					{
						//上
						evt.keyCode = JJoystickCode.UP;
					}
					else if(cy-ms_Distance > ms_centerY)
					{
						//下
						evt.keyCode = JJoystickCode.DOWN;
					}
						
				}
				if(evt.keyCode)
				{
					this.dispatchEvent(evt);
				}
				else
				{
					evt = null
				}
			}
		}
	}
}